package com.chen.gcode.utils;

import com.chen.gcode.models.PrintMode;
import com.chen.gcode.models.SpindleType;

import java.awt.*;
import java.io.*;
import java.math.BigDecimal;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;




public class GCodeGenerate {

    private String mode;

    private float speed; //移动速度
    private float power; //激光功率


    private String ncPath;

    private String spindleType;

    private List<PixLineItem> tempLines;

    private List<PixPointItem> pointItemList;

    private String printMode;

    public  int[][] generate(String bmpPath,String dstPath){
        try {
            // 创建读取文件的字节流
            FileInputStream fis = new FileInputStream(bmpPath);
            BufferedInputStream bis = new BufferedInputStream(fis);


            File saveFile = new File(dstPath);
            if (!saveFile.exists()){
                saveFile.createNewFile();
            }


//            G28 ;Home all axes
//                    G21 ;Set units to millimeters
//            G90 ;Use absolute coordinates
//                    G92 ;Coordinate Offset

            FileOutputStream fileOutputStream = new FileOutputStream(saveFile);


            String prepareCmd = "G28 ;Home all axes\r\n";
            fileOutputStream.write(prepareCmd.getBytes());
            prepareCmd = "G21 ;Set units to millimeters\r\n";
            fileOutputStream.write(prepareCmd.getBytes());
            prepareCmd = "G90 ;Use absolute coordinates\r\n";
            fileOutputStream.write(prepareCmd.getBytes());
            prepareCmd = "G92 ;Coordinate Offset\r\n";
            fileOutputStream.write(prepareCmd.getBytes());

            // 读取时丢掉前面的18位，
            // 读取图片的18~21的宽度

            byte[] fileHeadBytes = new byte[14];

            bis.read(fileHeadBytes);


            byte[] sizeBytes = new byte[4];
            bis.read(sizeBytes);
            int size = byte2Int(sizeBytes); //大小

            byte[] b = new byte[4];
            bis.read(b);
            // 读取图片的高度22~25
            byte[] b2 = new byte[4];
            bis.read(b2);

            // 得到图片的高度和宽度
            int width = byte2Int(b);
            int heigth = byte2Int(b2);

            // 使用数组保存得图片的高度和宽度

            int[][] date = new int[Math.abs(heigth)][Math.abs(width)];



            int skipnum = 0;
            if (width * 3 % 4 != 0) {
                skipnum = 4 - width * 3 % 4;
            }
            // 读取位图中的数据，位图中数据时从54位开始的，在读取数据前要丢掉前面的数据 读取图片数据

            bis.skip(28);

            for (int i = 0; i < date.length; i++) {


                for (int j = 0; j < date[i].length; j++) {

                    int x = i;
                    int y = j;

                    // bmp的图片在window里面世3个byte为一个像素
                    int blue = bis.read();
                    int green = bis.read() ;
                    int red = bis.read();
                    // 创建一个Color对象，将rgb作为参数传入其中
                    Color c = new Color(red, green, blue);
                    // Color c = new Color(blue,green,red);
                    // 将得到的像素保存到date数组中

                    int aver = (blue + green + red)/3;


                    if (aver > 255/2){
                        //打印点,

                    }



                    String cmd = "M03";

                    fileOutputStream.write(cmd.getBytes());

                    date[i][j] = c.getRGB();
                    cmd = "G01";
                    cmd += " ";
                    cmd += "X"+i+" ";
                    cmd += "Y"+j+" ";

                    cmd += "F" + blue + "";
                    cmd += " \r\n";

                    fileOutputStream.write(cmd.getBytes());
                }
                // 如果补0的个数不为0，则需要跳过这些补上的0
                if (skipnum != 0) {
                    bis.skip(skipnum);
                }
            }


            bis.close();
            fileOutputStream.close();
            return date;
        } catch (Exception e) {
            e.printStackTrace();

        }
        return null;
    }

    public void medianFilteringx(String bmpPath,String dstPath){

        try {
            // 创建读取文件的字节流
            FileInputStream fis = new FileInputStream(bmpPath);
            BufferedInputStream bis = new BufferedInputStream(fis);

            FileOutputStream out = new FileOutputStream(dstPath);

            // 读取时丢掉前面的18位，
            // 读取图片的18~21的宽度

            byte[] fileHeadBytes = new byte[14];

            bis.read(fileHeadBytes);
            out.write(fileHeadBytes);



            byte[] sizeBytes = new byte[4];
            bis.read(sizeBytes);
            out.write(sizeBytes);

            int size = byte2Int(sizeBytes); //大小

            byte[] b = new byte[4];
            bis.read(b);
            out.write(b);

            // 读取图片的高度22~25
            byte[] b2 = new byte[4];
            bis.read(b2);
            out.write(b2);

            // 得到图片的高度和宽度
            int width = byte2Int(b);  //宽有几个像素
            int heigth = byte2Int(b2); //高有几个像素

            // 使用数组保存得图片的高度和宽度

            int[][] date = new int[Math.abs(heigth)][Math.abs(width)];



            int skipnum = 0;
            if (width * 3 % 4 != 0) {
                skipnum = 4 - width * 3 % 4;
            }
            // 读取位图中的数据，位图中数据时从54位开始的，在读取数据前要丢掉前面的数据 读取图片数据

            byte[] skipdata = new byte[28];
            bis.read(skipdata);
            out.write(skipdata);
            List<PixLineItem> lins = new ArrayList<>();

            ByteBuffer buffer = ByteBuffer.allocate(Math.abs(heigth)*Math.abs(width)*6);
            for (int i = 0; i < date.length; i++) {

                Integer start = null;
                Integer end = null;

                for (int j = 0; j < date[i].length; j++) {
                    int x = i;
                    int y = j;
                    //读取第j个像素
                    // bmp的图片在window里面世3个byte为一个像素
                    int blue = bis.read();
                    int green = bis.read() ;
                    int red = bis.read();

                    // 创建一个Color对象，将rgb作为参数传入其中
                    Color c = new Color(red, green, blue);
                    // Color c = new Color(blue,green,red);
                    // 将得到的像素保存到date数组中

                    //读取第j个像素
                    int aver = (blue + green + red)/3;

                    if (aver < 240){
                        short a = 0;
                        buffer.putShort(a);
                        buffer.putShort(a);
                        buffer.putShort(a);
                    }else {

                        short a = 255;
                        buffer.putShort(a);
                        buffer.putShort(a);
                        buffer.putShort(a);
                    }
                }
                // 如果补0的个数不为0，则需要跳过这些补上的0
                if (skipnum != 0) {
                    byte[] sk = new byte[skipnum*6];
                    bis.read(sk);
                    buffer.put(sk);
                }
            }

            byte[] bytes = ImageUtils.medianFiltering(buffer.array(),width,heigth);

            out.write(bytes);
            bis.close();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public void updateToBlack(String bmpPath,String dstPath){

        try {
            // 创建读取文件的字节流
            FileInputStream fis = new FileInputStream(bmpPath);
            BufferedInputStream bis = new BufferedInputStream(fis);

            FileOutputStream out = new FileOutputStream(dstPath);

            // 读取时丢掉前面的18位，
            // 读取图片的18~21的宽度

            byte[] fileHeadBytes = new byte[14];

            bis.read(fileHeadBytes);
            out.write(fileHeadBytes);



            byte[] sizeBytes = new byte[4];
            bis.read(sizeBytes);
            out.write(sizeBytes);

            int size = byte2Int(sizeBytes); //大小

            byte[] b = new byte[4];
            bis.read(b);
            out.write(b);

            // 读取图片的高度22~25
            byte[] b2 = new byte[4];
            bis.read(b2);
            out.write(b2);

            // 得到图片的高度和宽度
            int width = byte2Int(b);  //宽有几个像素
            int heigth = byte2Int(b2); //高有几个像素

            // 使用数组保存得图片的高度和宽度

            int[][] date = new int[Math.abs(heigth)][Math.abs(width)];



            int skipnum = 0;
            if (width * 3 % 4 != 0) {
                skipnum = 4 - width * 3 % 4;
            }
            // 读取位图中的数据，位图中数据时从54位开始的，在读取数据前要丢掉前面的数据 读取图片数据

            byte[] skipdata = new byte[28];
            bis.read(skipdata);
            out.write(skipdata);
            List<PixLineItem> lins = new ArrayList<>();
            for (int i = 0; i < date.length; i++) {

                Integer start = null;
                Integer end = null;

                for (int j = 0; j < date[i].length; j++) {
                    int x = i;
                    int y = j;
                    //读取第j个像素
                    // bmp的图片在window里面世3个byte为一个像素
                    int blue = bis.read();
                    int green = bis.read() ;
                    int red = bis.read();

                    // 创建一个Color对象，将rgb作为参数传入其中
                    Color c = new Color(red, green, blue);
                    // Color c = new Color(blue,green,red);
                    // 将得到的像素保存到date数组中

                    //读取第j个像素
                    int aver = (blue + green + red)/3;

                    if (aver < 240){
                        out.write(0);
                        out.write(0);
                        out.write(0);
                    }else {

                        out.write(255);
                        out.write(255);
                        out.write(255);
                    }
                }
                // 如果补0的个数不为0，则需要跳过这些补上的0
                if (skipnum != 0) {
                    byte[] sk = new byte[skipnum];
                    bis.read(sk);
                    out.write(sk);
                }
            }
            bis.close();

            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }




    public  int[][] generateBlack(String bmpPath,String dstPath){
        try {
            // 创建读取文件的字节流
            FileInputStream fis = new FileInputStream(bmpPath);
            BufferedInputStream bis = new BufferedInputStream(fis);
            // 读取时丢掉前面的18位，
            // 读取图片的18~21的宽度

            byte[] fileHeadBytes = new byte[14];

            bis.read(fileHeadBytes);


            byte[] sizeBytes = new byte[4];
            bis.read(sizeBytes);
            int size = byte2Int(sizeBytes); //大小

            byte[] b = new byte[4];
            bis.read(b);
            // 读取图片的高度22~25
            byte[] b2 = new byte[4];
            bis.read(b2);

            // 得到图片的高度和宽度
            int width = byte2Int(b);  //宽有几个像素
            int heigth = byte2Int(b2); //高有几个像素

            // 使用数组保存得图片的高度和宽度

            int[][] date = new int[Math.abs(heigth)][Math.abs(width)];



            int skipnum = 0;
            if (width * 3 % 4 != 0) {
                skipnum = 4 - width * 3 % 4;
            }
            // 读取位图中的数据，位图中数据时从54位开始的，在读取数据前要丢掉前面的数据 读取图片数据

            bis.skip(28);
            List<PixLineItem> lins = new ArrayList<>();
            pointItemList = new ArrayList<>();
            for (int i = 0; i < date.length; i++) {

                Integer start = null;
                Integer end = null;

                for (int j = 0; j < date[i].length; j++) {

                    int x = i;
                    int y = j;

                    //读取第j个像素
                    // bmp的图片在window里面世3个byte为一个像素
                    int blue = bis.read();
                    int green = bis.read() ;
                    int red = bis.read();


                    // 创建一个Color对象，将rgb作为参数传入其中
                    Color c = new Color(red, green, blue);
                    // Color c = new Color(blue,green,red);
                    // 将得到的像素保存到date数组中

                    //读取第j个像素
                    int aver = (blue + green + red)/3;


                    if (printMode == PrintMode.PrintMode_Gray){
                        PixPointItem pixPointItem = new PixPointItem();
                        pixPointItem.X = x;
                        pixPointItem.Y = y;
                        pixPointItem.averColor = aver;
                        pointItemList.add(pixPointItem);
                    }



                    if (aver < 255/2){
                       //打印点
                        if (start == null){
                            start = j;
                        }

                    }else {
                       //非打印点
                        if (start != null){
                            if (end == null){
                                end = j;
                            }
                        }

                    }

                    if (j == date[i].length-1){
                        if (start != null && end == null){
                            end = start;
                        }
                    }

                    if (start != null && end != null){

                        if (end-start == 0){
                            PixLineItem item = new PixLineItem();
                            item.startX = start;
                            item.startY = i;
                            item.endX = end;
                            item.endY = i;
                            lins.add(item);

//                            System.out.println(String.format("线段位置 (%s,%s)-(%s,%s)",i,start,i,end));
                        }else{
//                            System.out.println(String.format("线段位置 (%s,%s)-(%s,%s)",i,start,i,end-1));
                            PixLineItem item = new PixLineItem();
                            item.startX = start;
                            item.startY = i;
                            item.endX = (end-1);
                            item.endY = i;
                            lins.add(item);
                        }


                        start = null;
                        end = null;
                    }
                }
                // 如果补0的个数不为0，则需要跳过这些补上的0
                if (skipnum != 0) {
                    bis.skip(skipnum);
                }
            }
            tempLines = lins;


            if (printMode == PrintMode.PrintMode_Gray){
                pointsToGcode(dstPath);
            }else {
                linesToGcode(dstPath);
            }


            bis.close();
            return date;
        } catch (Exception e) {
            e.printStackTrace();

        }
        return null;
    }

    public void pointsToGcode(String dstPath){
        try {
            File saveFile = new File(dstPath);
            if (!saveFile.exists()){
                saveFile.createNewFile();
            }


//            G28 ;Home all axes
//                    G21 ;Set units to millimeters
//            G90 ;Use absolute coordinates
//                    G92 ;Coordinate Offset

            FileOutputStream fileOutputStream = new FileOutputStream(saveFile);
            BigDecimal pD = new BigDecimal(Setting.getInstance().getPointSize());
            double pointSize = pD.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

            String prepareCmd = "G28 ;Home all axes\r\n";
            fileOutputStream.write(prepareCmd.getBytes());
            prepareCmd = "G21 ;Set units to millimeters\r\n";
            fileOutputStream.write(prepareCmd.getBytes());
            prepareCmd = "G90 ;Use absolute coordinates\r\n";
            fileOutputStream.write(prepareCmd.getBytes());
            prepareCmd = "G92 ;Coordinate Offset\r\n";
            fileOutputStream.write(prepareCmd.getBytes());
            if (spindleType == SpindleType.SpindleType_Laser){
                prepareCmd = "M03 S"+"5"+"\r\n";
                fileOutputStream.write(prepareCmd.getBytes());
            }else {
                prepareCmd = "G01 Z"+Setting.getInstance().getSafeDistance()+"\r\n";
                fileOutputStream.write(prepareCmd.getBytes());
            }

            String cmd = "";
            if (pointItemList != null && pointItemList.size() >0){
                while (pointItemList.size() > 0){
                    PixPointItem item = pointItemList.get(0);



                    if (spindleType == SpindleType.SpindleType_Laser){
                        cmd = "M03 S"+(int)(Integer.valueOf(Setting.getInstance().getPower()) * (item.averColor/255.00))+" \r\n";
                        fileOutputStream.write(cmd.getBytes());
                    }else {
                        cmd = "G01 Z0 "+"\r\n";
                        fileOutputStream.write(cmd.getBytes());
                    }

                    cmd = "G00";
                    cmd += " ";
                    cmd += "X"+item.X * pointSize+" ";
                    cmd += "Y"+item.Y* pointSize+" ";
                    cmd += "F"+Setting.getInstance().getSpeed()+" ";
                    cmd += " \r\n";

                    fileOutputStream.write(cmd.getBytes());

                    if (spindleType == SpindleType.SpindleType_Laser){
                        cmd = "M03 S"+"5"+"\r\n";
                        fileOutputStream.write(cmd.getBytes());
                    }else {
                        cmd = "G01 Z"+Setting.getInstance().getSafeDistance()+"\r\n";
                        fileOutputStream.write(cmd.getBytes());
                    }
                    pointItemList.remove(item);
                }

            }

            if (spindleType == SpindleType.SpindleType_Laser){
                prepareCmd = "M05 \r\n";
                fileOutputStream.write(prepareCmd.getBytes());
            }else {
                prepareCmd = "G01 Z"+Setting.getInstance().getSafeDistance()+"\r\n";
                fileOutputStream.write(prepareCmd.getBytes());
            }

            prepareCmd = "G0 X0.000 Y0.000 \r\n";
            fileOutputStream.write(prepareCmd.getBytes());
            fileOutputStream.close();

        }catch (Exception e){
            e.printStackTrace();
        }
    }


    public void linesToGcode(String dstPath){


        try {
            File saveFile = new File(dstPath);
            if (!saveFile.exists()){
                saveFile.createNewFile();
            }


//            G28 ;Home all axes
//                    G21 ;Set units to millimeters
//            G90 ;Use absolute coordinates
//                    G92 ;Coordinate Offset

            FileOutputStream fileOutputStream = new FileOutputStream(saveFile);


            String prepareCmd = "G28 ;Home all axes\r\n";
            fileOutputStream.write(prepareCmd.getBytes());
            prepareCmd = "G21 ;Set units to millimeters\r\n";
            fileOutputStream.write(prepareCmd.getBytes());
            prepareCmd = "G90 ;Use absolute coordinates\r\n";
            fileOutputStream.write(prepareCmd.getBytes());
            prepareCmd = "G92 ;Coordinate Offset\r\n";
            fileOutputStream.write(prepareCmd.getBytes());
            if (spindleType == SpindleType.SpindleType_Laser){
                prepareCmd = "M03 S"+"5"+"\r\n";
                fileOutputStream.write(prepareCmd.getBytes());
            }else {
                prepareCmd = "G01 Z"+Setting.getInstance().getSafeDistance()+"\r\n";
                fileOutputStream.write(prepareCmd.getBytes());
            }

            int x = 0; // 当前激光坐标
            int y = 0;

            BigDecimal pD = new BigDecimal(Setting.getInstance().getPointSize());
            double pointSize = pD.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

            while (tempLines.size()> 0){
                PixLineItem item = getNearLine(x,y);

                Point startP = new Point(item.startX, item.startY);// 定义第一个点的坐标(5,6)
                Point endP = new Point(item.endX,item.endY);// 定义第二个点的坐标(7,8)

                Point jgP = new Point(x,y);// 定义第二个点的坐标(7,8)


                double startDis = Math.sqrt(Math.abs((startP.getX() - jgP.getX())* (startP.getX() - jgP.getX())+(startP.getY() - jgP.getY())* (startP.getY() - jgP.getY())));

                double endDis = Math.sqrt(Math.abs((endP.getX() - jgP.getX())* (endP.getX() - jgP.getX())+(endP.getY() - jgP.getY())* (endP.getY() - jgP.getY())));

                if (startDis < endDis){
                    //移动到 start 开激光
//
//                    System.out.println(String.format("移动到 (%s,%s) 开",startP.x,startP.y));
//                    //移动到 end 关激光
//                    System.out.println(String.format("移动到 (%s,%s) 关",endP.x,endP.y));
                    x = endP.x;
                    y = endP.y;




                    String cmd = "";

//                    fileOutputStream.write(cmd.getBytes());




                    cmd = "G00";
                    cmd += " ";
                    cmd += "X"+startP.x * pointSize+" ";
                    cmd += "Y"+startP.y* pointSize+" ";
                    cmd += "F"+Setting.getInstance().getSpeed()+" ";
                    cmd += " \r\n";

                    fileOutputStream.write(cmd.getBytes());

                    if (spindleType == SpindleType.SpindleType_Laser){
                        cmd = "M03 S"+Setting.getInstance().getPower()+"\r\n";
                        fileOutputStream.write(cmd.getBytes());
                    }else {
                        cmd = "G01 Z0 "+"\r\n";
                        fileOutputStream.write(cmd.getBytes());
                    }



                    cmd = "G01";
                    cmd += " ";
                    cmd += "X"+endP.x* pointSize+" ";
                    cmd += "Y"+endP.y* pointSize+" ";
                    cmd += "F"+Setting.getInstance().getSpeed()+" ";
                    cmd += " \r\n";

                    fileOutputStream.write(cmd.getBytes());

                    if (spindleType == SpindleType.SpindleType_Laser){
                        cmd = "M03 S"+"5"+"\r\n";
                        fileOutputStream.write(cmd.getBytes());
                    }else {
                        cmd = "G01 Z"+Setting.getInstance().getSafeDistance()+"\r\n";
                        fileOutputStream.write(cmd.getBytes());
                    }


                }else {
                    //移动到 end 开激光

                    //移动到 start 关激光

//                    System.out.println(String.format("移动到 (%s,%s) 开",endP.x,endP.y));
//                    System.out.println(String.format("移动到 (%s,%s) 关",startP.x,startP.y));
                    x = startP.x;
                    y = startP.y;


                    String cmd = "";

                    fileOutputStream.write(cmd.getBytes());

                    cmd = "G01";
                    cmd += " ";
                    cmd += "X"+endP.x* pointSize+" ";
                    cmd += "Y"+endP.y* pointSize+" ";
                    cmd += "F"+Setting.getInstance().getSpeed()+" ";
                    cmd += " \r\n";

                    fileOutputStream.write(cmd.getBytes());

                    if (spindleType == SpindleType.SpindleType_Laser){
                        cmd = "M03 S"+Setting.getInstance().getPower()+"\r\n";
                        fileOutputStream.write(cmd.getBytes());
                    }else {
                        cmd = "G01 Z0 "+"\r\n";
                        fileOutputStream.write(cmd.getBytes());
                    }


                    cmd = "G01";
                    cmd += " ";
                    cmd += "X"+startP.x* pointSize+" ";
                    cmd += "Y"+startP.y* pointSize+" ";
                    cmd += "F"+Setting.getInstance().getSpeed()+" ";
                    cmd += " \r\n";
                    fileOutputStream.write(cmd.getBytes());

                    if (spindleType == SpindleType.SpindleType_Laser){
                        cmd = "M03 S"+"5"+"\r\n";
                        fileOutputStream.write(cmd.getBytes());
                    }else {
                        cmd = "G01 Z"+Setting.getInstance().getSafeDistance()+"\r\n";
                        fileOutputStream.write(cmd.getBytes());
                    }
                }
            }

            if (spindleType == SpindleType.SpindleType_Laser){
                prepareCmd = "M05 \r\n";
                fileOutputStream.write(prepareCmd.getBytes());
            }else {
                prepareCmd = "G01 Z"+Setting.getInstance().getSafeDistance()+"\r\n";
                fileOutputStream.write(prepareCmd.getBytes());
            }

            prepareCmd = "G0 X0.000 Y0.000 \r\n";
            fileOutputStream.write(prepareCmd.getBytes());
            fileOutputStream.close();

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public PixLineItem getNearLine(Integer x,Integer y){
        PixLineItem line = null;

        if (mode == "line"){
            if (tempLines!=null && tempLines.size() >0)
                line = tempLines.get(0);
        }else if (mode == "quick"){
            if (tempLines!=null && tempLines.size() >0)
            line = tempLines.get(0);

            if (line.startY %2 == 1){
                int tempX = line.startX;
                int tempY = line.startY;

                line.startX = line.endX;
                line.startY = line.endY;

                line.endX = tempX;
                line.endY = tempY;
            }


        }else if (mode == "best"){
            double destance = Double.MAX_VALUE;
            for (PixLineItem item :tempLines
            ) {
                Point startP = new Point(item.startX, item.startY);// 定义第一个点的坐标(5,6)
                Point endP = new Point(item.endX,item.endY);// 定义第二个点的坐标(7,8)

                Point jgP = new Point(x,y);// 定义第二个点的坐标(7,8)


                double startDis = Math.sqrt(Math.abs((startP.getX() - jgP.getX())* (startP.getX() - jgP.getX())+(startP.getY() - jgP.getY())* (startP.getY() - jgP.getY())));

                double endDis = Math.sqrt(Math.abs((endP.getX() - jgP.getX())* (endP.getX() - jgP.getX())+(endP.getY() - jgP.getY())* (endP.getY() - jgP.getY())));

                double juli = startDis;
                if (endDis < juli){
                    juli = endDis;
                }

                if (juli<destance){
                    destance = juli;
                    line = item;
                }
            }
        }



        tempLines.remove(line);
        return line;
    }

    public void changeCode(String bmpPath,String dstPath){

        File file = new File(bmpPath);
        String dataString = FileUtil.readStringFile(file.toURI());
        dataString = "G28 \r\n"+"M5 \r\n"+dataString;
        dataString = dataString.replaceAll("M3(.*)\r\n","M03 S"+Setting.getInstance().getPower()+"\r\n");
        dataString = dataString.replaceAll("S(.*)\r\n","S"+Setting.getInstance().getSpeed()+"\r\n");
        dataString = dataString.replaceAll("F(.*)\r\n","S"+Setting.getInstance().getSpeed()+"\r\n");
//        dataString = dataString.replaceAll("M03(.*)\r\n","XXX\r\n");
//        dataString = dataString.replaceAll("M03(.*)\r\n","XXX\r\n");
        System.out.println(dataString);


        try {
            // 创建读取文件的字节流
            FileInputStream fis = new FileInputStream(bmpPath);
            BufferedInputStream bis = new BufferedInputStream(fis);


            File saveFile = new File(dstPath);
            if (!saveFile.exists()){
                saveFile.createNewFile();
            }


//            G28 ;Home all axes
//                    G21 ;Set units to millimeters
//            G90 ;Use absolute coordinates
//                    G92 ;Coordinate Offset

            FileOutputStream fileOutputStream = new FileOutputStream(saveFile);


            fileOutputStream.write(dataString.getBytes());
            bis.close();
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();

        }
    }


    public void testCode(String dstPath){


        String dataString = "";
        dataString = ""
                +"S6000 \r\n"
                +"G92 X0 Y0 Z0 \r\n"
                +"G21 \r\n"
                +"G90 \r\n"
                +"G1 F"+Setting.getInstance().getSpeed()+" \r\n"
                + "G28 \r\n"
                +"M5 \r\n"

                +"G1 X5 Y5 \r\n";
        int x = 5;
        int y = 0;
        for (int i = 0; i < 255; i++) {

            int fix = 0;

            fix = (i/35) * 6;

            if (i%2 ==0){
                y = 5 + fix;
            }
            else {
                y = 10 + fix;
            }
            x = 5+i%35;
            dataString = dataString + "M03 S" + i + " \r\n";
            dataString = dataString + "G1 X"+x+ " Y"+y+" \r\n";
        }

//        dataString = dataString.replaceAll("M3(.*)\r\n","M03 S"+Setting.getInstance().getPower()+"\n");
//        dataString = dataString.replaceAll("S(.*)\r\n","S"+Setting.getInstance().getSpeed()+"\n");
//        dataString = dataString.replaceAll("M03(.*)\r\n","XXX\r\n");
//        dataString = dataString.replaceAll("M03(.*)\r\n","XXX\r\n");
        System.out.println(dataString);
        try {
            File saveFile = new File(dstPath);
            if (!saveFile.exists()){
                saveFile.createNewFile();
            }
            FileOutputStream fileOutputStream = new FileOutputStream(saveFile);
            fileOutputStream.write(dataString.getBytes());
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();

        }
    }



    // 将四个byte拼接成一个int
    public  int byte2Int(byte[] by) {
        int t1 = by[3] & 0xff;
        int t2 = by[2] & 0xff;
        int t3 = by[1] & 0xff;
        int t4 = by[0] & 0xff;
        int num = t1 << 24 | t2 << 16 | t3 << 8 | t4;
        return num;

    }


    public float getSpeed() {
        return speed;
    }

    public void setSpeed(float speed) {
        this.speed = speed;
    }

    public float getPower() {
        return power;
    }

    public void setPower(float power) {
        this.power = power;
    }

//    public static void main(String[] args) {
//        String origin = "M03S30\r\nM03 S100\r\nVVDR";
//        origin = origin.replaceAll("M03(.*)\r\n","XXX\r\n");
//        System.out.println(origin);
//    }



    public String getMode() {
        return mode;
    }

    public void setMode(String mode) {
        this.mode = mode;
    }

    public String getSpindleType() {
        return spindleType;
    }

    public void setSpindleType(String spindleType) {
        this.spindleType = spindleType;
    }


    public String getPrintMode() {
        return printMode;
    }

    public void setPrintMode(String printMode) {
        this.printMode = printMode;
    }

    private class PixLineItem{
        public Integer startX;
        public Integer startY;
        public Integer endX;
        public Integer endY;
    }

    private class PixPointItem{
        public Integer X;
        public Integer Y;
        public int averColor;
    }



}
